package com.akkainaction.mailbox.custommailbox.queue;

import akka.actor.ActorRef;
import akka.dispatch.Envelope;
import akka.dispatch.MessageQueue;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public class BusinessMsgQueue implements MessageQueue {
    private Queue<Envelope> queue = new ConcurrentLinkedQueue<Envelope>();

    @Override
    public void enqueue(ActorRef actorRef, Envelope envelope) {
        System.out.println("----enqueue----");
        queue.offer(envelope);
    }

    @Override
    public Envelope dequeue() {
        System.out.println("----dequeue----");
        return queue.poll();
    }

    @Override
    public int numberOfMessages() {
        return queue.size();
    }

    @Override
    public boolean hasMessages() {
        return !queue.isEmpty();
    }

    @Override
    public void cleanUp(ActorRef owner, MessageQueue deadLetters) {
        for (Envelope el : queue) {
            deadLetters.enqueue(owner, el);
        }
    }
}
